

% load data
data2   = csvread('source/LaborMarketBasics.csv');
EUd     = data2(:,2) ;
UEd     = data2(:,3) ;
wt      = data2(:,5) ;
ud      = data2(:,6) ;

% weights to de-mean
w0 = ones(size(wt)) ;
w0 = w0 / sum(w0) ;

lEUd = log(EUd) ;
lUEd = log(UEd) ;

lEUd = lEUd - sum( w0 .* lEUd ) / sum(w0) ;
lUEd = lUEd - sum( w0 .* lUEd ) / sum(w0) ;

lUcd = lEUd - lUEd ;

% model
w = ones(size(popC)) / size(popC,1) ;

lEU = log(sC) - sum( w .* log(sC) ) ;
lUE = log(fC) - sum( w .* log(fC) ) ;
lUc = lEU - lUE ;

[~,i0] = min( lUc.^2 ) ;
lEU = lEU - lEU(i0) ;
lUE = lUE - lUE(i0) ;

luc = linspace(-0.5,0.5,size(vC,1)) ;

fontsize = 14 ;

if ishandle(1)
    close(1)
end


%% figure for EU rates

figure(1)
set(gcf,'Position',[100 100 600 500])

sEU = scatter(lUcd,lEUd, wt / 500 ,MyBlue,'LineWidth',1); 
sEU.MarkerEdgeAlpha = .5 ; hold on;
plot(lUc,lEU,'LineWidth',10,'Color',MyBlue); hold on;
plot(luc,luc,'r-','Color',MyOrange)

xlabel('log unemployment / employment') 
ylabel('log job losing rate') 

legend('Data','Model','45 degree line','FontSize',fontsize,'Location','SouthEast') ;
legend boxoff

AxisFonts(0.95*fontsize,fontsize,0.95*fontsize,fontsize)

xlim([-0.5,0.5])
ylim([-0.5,0.5])

if options.SaveResults
    
    % pdf
    filename = 'results/FigureScatterEU' ;
    run printPDF.m ;
    
    % tiff
    exportgraphics(gcf,'results/FigureScatterEU.tiff','Resolution',1000) 

end

hold off



%% figure for UE rates

figure(11)
set(gcf,'Position',[100 100 600 500])

sUE = scatter(lUcd,-lUEd, wt / 500 ,MyBlue,'LineWidth',1); 
sUE.MarkerEdgeAlpha = .5 ; hold on;
plot(lUc,-lUE,'LineWidth',10,'Color',MyBlue); hold on;
plot(luc,luc,'r-','Color',MyOrange)

xlabel('log unemployment / employment') 
ylabel('- log job finding rate') 
AxisFonts(0.95*fontsize,fontsize,0.95*fontsize,fontsize)

xlim([-0.5,0.5])
ylim([-0.5,0.5])

if options.SaveResults

    % pdf
    filename = 'results/FigureScatterUE' ;
    run printPDF.m ;
    
    % tiff
    exportgraphics(gcf,'results/FigureScatterUE.tiff','Resolution',1000) 

end

hold off




%% figure for tightness

% tightness
[~,i0C] = min( lUc.^2  ) ;
ltightnessC = log( Theta( vC , zetaC ) ) ;
ltightnessC = ltightnessC - ltightnessC(i0C) ;

wA = ones(size(popA)) / size(popA,1) ;
lEUA = log(sA) - sum( wA .* log(sA) ) ;
lUEA = log(fA) - sum( wA .* log(fA) ) ;
lUcA = lEUA - lUEA ;
[~,i0A] = min( lUcA.^2  ) ;

ltightnessA = log( Theta( vA , zetaA ) ) ;
ltightnessA = ltightnessA - ltightnessA(i0A) ;

% regression lines in data
meanU = 0.08 ;
coefNoJtJ  = - 0.06 ;
slopeNoJtJ = -0.5121205 ;
coefJtJ    = 0.05 ;
slopeJtJ = .4677052 ; 

figure(100)
set(gcf,'Position',[100 100 600 500])

plot(luc , slopeNoJtJ * luc ,'--','LineWidth',2,'Color',MyBlue); hold on;
plot(luc , slopeJtJ * luc   ,'-.','LineWidth',2,'Color',MyOrange); hold on;
plot(lUc , ltightnessC      ,'-.','LineWidth',5,'Color',MyOrange); hold on;
plot(lUcA, ltightnessA      ,'--','LineWidth',5,'Color',MyBlue); hold on;

xlabel('log unemployment / employment') 
ylabel('log labor market tightness')

leg = legend('Data: OLS w/o JtJ correction',...
       'Data: OLS w/ JtJ correction' ,...
       'Model: baseline parameters', ...
       'Model: $\gamma = 3$', 'FontSize',fontsize,'Location','SouthEast') ;
set(leg,'interpreter','latex')
legend boxoff

xlim([-0.5,0.5])
ylim([-0.5,0.5])

AxisFonts(0.95*fontsize,fontsize,0.95*fontsize,fontsize)

if options.SaveResults
    
    % pdf
    filename = 'results/FigureTightness' ;
    run printPDF.m ;
    
    % tiff
    exportgraphics(gcf,'results/FigureTightness.tiff','Resolution',1000) 

end

hold off


% regressions
iregC = abs(lUc) < 0.5 ;
iregA = abs(lUcA) < 0.5 ;

fitlm( lUc(iregC) , ltightnessC(iregC) )  % 0.24178
fitlm( lUcA(iregA) , ltightnessA(iregA) ) % -0.69621


%% figure vacancy meeting rate

% vacancy fill rate in model
lqRC = - Params.alpha * ltightnessC + zetaC .* log( Params.B0 ./ ( ( Params.b + vC )) ) ;
lqRC = lqRC - lqRC(i0C) ; 

lqRA = - Params.alpha * ltightnessA + zetaA .* log( Params.B0 ./ ( ( Params.b + vA )) ) ;
lqRA = lqRA - lqRA(i0A); 

% vacancy meeting rate in model
lqC = - Params.alpha * ltightnessC ;
lqC = lqC - lqC(i0C) ; 

lqA = - Params.alpha * ltightnessA ;
lqA = lqA - lqA(i0A);

% vacancy filling rate in data (Kuhn et al. 2021)
lqRdata = log(linspace(0.2,0.28,100)) ;
udata   = linspace(0.04,0.2,100) ;
lUcdata = log(udata./(1-udata))  ;
lUcdata = lUcdata - mean(lUcdata) ;
[~,i0data]  = min(lUcdata.^2) ;
lqRdata = lqRdata - lqRdata(i0data) ;

% regression slopes
slopeqNoJtJ = - Params.alpha * slopeNoJtJ ;
slopeqJtJ   = - Params.alpha * slopeJtJ ;



% figure: only meeting rates
figure(101)
set(gcf,'Position',[100 100 600 500])

plot(lUc , slopeqNoJtJ * lUc,'--','LineWidth',2,'Color',MyBlue); hold on;
plot(lUc , slopeqJtJ * lUc,'-.','LineWidth',2,'Color',MyOrange); hold on;

plot(lUc, lqC,'-.'  ,'LineWidth',5,'Color',MyOrange); hold on;

plot(lUcA, lqA,'--'  ,'LineWidth',5,'Color',MyBlue); hold on;

xlabel('log unemployment /employment')
ylabel('log vacancy meeting rate')

leg = legend('Data: meeting rate w/o JtJ correction','Data: meeting rate w/ JtJ correction',...
       'Model: baseline parameters','Model: $\gamma=3$',...
       'FontSize',fontsize,'Location','SouthEast') ;
set(leg,'interpreter','latex')
legend boxoff

xlim([-0.5,0.5])
ylim([-0.5,0.5])

AxisFonts(0.95*fontsize,fontsize,0.95*fontsize,fontsize)

if options.SaveResults
    
    % pdf
    filename = 'results/FigureVacMeetingRate' ;
    run printPDF.m ;
    
    % tiff
    exportgraphics(gcf,'results/FigureVacMeetingRate.tiff','Resolution',1000) 

end

hold off






